package com.hjq.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
// 将类交给Spring容器管理
@Component
// 实现类HandlerInterceptor
public class DemoInterceptor implements HandlerInterceptor {

    @Override
    // 目标资源方法执行前执行，放行，返回true，拦截，返回false
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle");
        // return HandlerInterceptor.super.preHandle(request, response, handler);
        return true;
    }

    @Override
    // 目标资源方法结束后执行
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle");
    }


    @Override
    // 视图渲染结束后执行，多用于早期前后端不分离项目，如今基本不用
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion");
    }
}
